<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class kas_bank extends Default_My_Model {

    private $QRY = "SELECT a.*, b.nama_bank, c.nama        
        FROM kas_bank a
        LEFT JOIN master_bank b on (a.id_bank=b.id_bank)
        LEFT JOIN mt_karyawan c on (a.submitter_kb=c.nik) ";
    
   private $QRY2 = "SELECT a.*, b.nama_bank, c.nama, 
        @km_kb := a.kas_masuk_kb as kas_masuk, @kk_kb := a.kas_keluar_kb as kas_keluar, @saldo_kb := @saldo_kb + @km_kb - @kk_kb AS saldo
        FROM kas_bank a
        LEFT JOIN master_bank b on (a.id_bank=b.id_bank)
        LEFT JOIN mt_karyawan c on (a.submitter_kb=c.nik) ";

    private $QRYSALDO = "SELECT (SUM(kas_masuk_kb) - SUM(kas_keluar_kb)) as saldo FROM kas_bank a
        LEFT JOIN master_bank b on (a.id_bank=b.id_bank)";

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    function initSaldo($whr, $offset = 0, $length= 30, $start_date) {        
        $result = $this->getKasBank($whr, $offset, $length);
        $total = 0;
        foreach ($result->result() as $row) {             
            $total = $this->get_scalar("select sum(kas_masuk_kb) - sum(kas_keluar_kb) as total from kas_bank where id_kb < $row->id_kb","total");        
            break;
        }      
        if ($total == null){
            $total = 0;            
        }        
        $this->db->query("select @saldo_kb:=".$total." as x");
    }
    
    public function getAllKasBank($whr) {
        $resultSet = array();
        if ($whr != "") {
            $resultSet['data'] = $this->db->query($this->QRY . $whr . " order by date(a.tanggal_kb) DESC");
        } else {
            $resultSet['data'] = $this->db->query($this->QRY . " order by date(a.tanggal_kb) DESC");
        }
        $resultSet['last_query'] = $this->db->last_query();
        return $resultSet;
    }

    public function getKasBank($whr, $sc_pageslimit, $offset) {
        
        if ($whr != "") {
            $temp = $this->QRY2  . $whr. " order by date(a.tanggal_kb) asc, a.id_kb LIMIT " . $sc_pageslimit . " OFFSET " . $offset;
        } else {
            $temp = $this->QRY2. " order by date(a.tanggal_kb), a.id_kb LIMIT " . $sc_pageslimit . " OFFSET " . $offset;
        }                
        $resultSet = $this->db->query($temp);
        return $resultSet;

    }

    public function addKasBank($arrVals) {
        $this->db->insert('kas_bank', $arrVals);
    }

    public function updateKasBank($id, $arrVals) {
        $this->db->where('id_kb', $id);
        $this->db->update("kas_bank", $arrVals);
    }

    public function getKasBankDetil($kode) {
        $conds = " where id_kb = '" . $kode . "'";
        $resultSet = $this->db->query($this->QRY . "" . $conds);
        return $resultSet;
    }

    public function removeKasBank($id) {
        $this->db->where('id_kb', $id);
        $this->db->delete('kas_bank', $where);
    }

    public function getSaldo($whr) {
        $resultSet = array();
        if ($whr != "") {
            $this->QRY  = $this->QRYSALDO . $whr ;
        } else {
            $this->QRY  = $this->QRYSALDO;
        }
        $resultSet = $this->db->query($this->QRY );
        return $resultSet;
    }
    
    public function getSaldoAwalBulan($whr) {
        $resultSet = array();
        if ($whr != "") {
            $this->QRY  = $this->QRYSALDO . $whr ;
        } else {
            $this->QRY  = $this->QRYSALDO;
        }        
        $resultSet = $this->db->query($this->QRY );
        return $resultSet;
    }

}
?>